<?php

namespace app\admin\controller;

use think\Cache;
use think\Controller;

class Common extends Controller
{

    /**
     * 清除缓存
     */
    public function delCache(){
        // 清文件缓存
        $dirs = [ROOT_PATH.'runtime/'];
        @mkdir('runtime',0777,true);
        foreach($dirs as $dir) {
            @unlink($dir);
        }
        // 清理缓存
        Cache::clear();

        $this->success('清除缓存成功！');
    }

    /**
     * 推荐 操作
     */
    public function setRecommend(){
        $table = $this->request->param('table');
        $id = $this->request->param('id');
        $isRecommend = $this->request->param('isRecommend');
        if(!$id || !$isRecommend){
            return json(['code' => 0, 'msg' => '参数错误！']);
        }

        $model = model($table);
        $data = [
            'isRecommend' => $isRecommend
        ];

        $result = $model->allowField(true)->save($data, ['id' => $id]);
        if($result !== false){
            if($isRecommend == 1){
                $message = '首页推荐成功！';
            }else{
                $message = '取消推荐成功！';
            }

            return json(['code' => 1, 'msg' => $message]);
        }else{
            if($isRecommend == 1){
                $message = '首页推荐失败！';
            }else{
                $message = '取消推荐失败！';
            }

            return json(['code' => 0, 'msg' => $message]);
        }
    }
}